package com.KA.controller;


import com.KA.common.R;
import com.KA.dto.AccountDto;
import com.KA.entity.Account;
import com.KA.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/zj/user")
@Api(tags = "记账控制类")
public class AccountController {

    @Autowired
    AccountService accountService;

    @ApiOperation("新增账单")
    @PostMapping("/addji")
    public R<String> addji(@RequestBody Account account){
        Boolean savezj = accountService.savezj(account);
        if (savezj){
            return R.success("保存记账成功");
        }else{
            return R.error("保存记账失败,请查看余额是否充足");
        }
    }

    @ApiOperation("账单列表")
    @PostMapping("/zlist")
    public R<List<Account>> list(@RequestBody AccountDto accountDto) throws ParseException {
        List<Account> list = accountService.zlist(accountDto);
        return R.success(list);
    }

    @ApiOperation("账单详细")
    @GetMapping("/findZhangById/{id}")
    public R<Account> list(@PathVariable Long id)  {
     Account account = accountService.getById(id);
        if (account != null){
            return R.success(account);
        }
        return R.error("没有查询到对应信息");
    }

    @ApiOperation("修改账单")
    @PutMapping
    public R<String> updateAccount(@RequestBody Account account){
        boolean save = accountService.updateById(account);
        if(save){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @ApiOperation("删除记账数据")
    @GetMapping("/deleteAccountByid/{id}")
    public R<String> deleteAccountByid(@PathVariable Long id) {
        if (accountService.deleteAccount(id)) {
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    @ApiOperation("月支出")
    @GetMapping("/accountPay")
    public R<String> accountPay()  {
         String a=accountService.accountPay();
        return R.success(a);
    }

    @ApiOperation("月收入")
    @GetMapping("/accountGet")
    public R<String> accountGet()  {
        String a=accountService.accountGet();
        return R.success(a);
    }



}
